package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.GeologicalQueryParams;
import com.css.fxfzypg.modules.baseData.repository.GeologicalRepository;
import com.css.fxfzypg.modules.baseData.entity.GeologicalEntity;
import com.css.fxfzypg.modules.baseData.service.GeologicalService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-16 13:49
 */
@Service
public class GeologicalServiceImpl implements GeologicalService {


    @Resource
    private GeologicalRepository geologicalRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GeologicalEntity saveGeological(GeologicalEntity geologicalEntity) {
        geologicalEntity.setDgdIsdelete(YNEnum.Y.toString());
        geologicalEntity.setDgdCreatetime(PlatformDateUtils.getCurrentTimestamp());

        return geologicalRepository.saveGeological(geologicalEntity);
    }

    @Override
    public Object getGeologicalById(String id) {

        return geologicalRepository.getGeologicalById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGeological(List<String> ids) {
        List<Integer> idss=new ArrayList<>();
        for (String id : ids) {
          idss.add(Integer.parseInt(id));
        }
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        geologicalRepository.deleteGeological(idss,currentTimestamp);
    }

    @Override
    public JSONObject getAllGeologicalByParams(GeologicalQueryParams queryParams) {
      return   PlatformPageUtils.formatPageData(geologicalRepository.getAllGeologicalByParams(queryParams));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response)throws IOException {
        List<GeologicalEntity> list = geologicalRepository.exportExcel();
        FileUtil.exportExcel(list, "地质灾害数据", "sheet", GeologicalEntity.class, "GeologicalExcel", response);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file)throws IOException {
        List<GeologicalEntity> geologicalEntities = FileUtil.importExcel(file, GeologicalEntity.class);
        for (GeologicalEntity geologicalEntity : geologicalEntities) {
            geologicalEntity.setDgdCreatetime(PlatformDateUtils.getCurrentTimestamp());
            geologicalEntity.setDgdIsdelete(YNEnum.Y.toString());
        }
        geologicalRepository.importExcel(geologicalEntities);
    }
}
